#include "widget.h"
#include "./ui_widget.h"
#include <iostream>
#include <QPalette>
#include <QDebug>
#include "LampManager.h"
#include "F3LampInterfaces.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    initUi();

    timer = new QTimer(this);
    timer_5ms = new QTimer(this);
    timer_100ms = new QTimer(this);

    connect(timer, &QTimer::timeout, this, &Widget::onTimer_10ms);
    connect(timer_5ms, &QTimer::timeout, this, &Widget::onTimer_5ms);
    connect(timer_100ms, &QTimer::timeout, this, &Widget::onTimer_100ms);

    g_Widget = this;

    InitApplication();

    timer_5ms->start(5);
    timer->start(10);
    timer_100ms->start(100);
}

Widget::~Widget()
{
    delete ui;
}

void Widget::onTimer_10ms()
{
    OnAppTask_10ms();
}

void Widget::onTimer_5ms()
{
    OnAppTask_5ms();
}

void Widget::onTimer_100ms()
{
    OnAppTask_100ms();
}

void Widget::Lamp_Status_Ctrl(QLineEdit* lamp, bool value)
{
    QPalette palette ;
    uint8_t ctrl = value ? 0 : 255;
    palette .setColor(QPalette::Base, QColor(255, ctrl, 255)); //背景色
    lamp->setAutoFillBackground(true);//开启背景色调整
    lamp->setPalette(palette);
}

void Widget::Lamp_Pwm_Ctrl(QLineEdit* lamp, uint8_t pwd)
{
    QPalette palette ;
    palette .setColor(QPalette::Base, QColor(255, 255-pwd, 255)); //背景色
    lamp->setAutoFillBackground(true);//开启背景色调整
    lamp->setPalette(palette);
}


QString Widget::createOutSigalName(const uint8_t& value)
{
    switch (value)
    {
    case 0: return QString("0x0：Initial");
    case 1: return QString("0x1：Off");
    case 2: return QString("0x2：On");
    case 3: return QString("0x3：Invalid");
    default:return QString("Err：Undefine Sig");
    }
}


QString Widget::createOutFaultName(const uint8_t& value)
{
    switch (value)
    {
    case 0: return QString("0x0：Initial");
    case 1: return QString("0x1：No Fault");
    case 2: return QString("0x2：Fault");
    case 3: return QString("0x3：Invalid");
    default:return QString("Err：Undefine Fault");
    }
}

uint16_t Widget::getTextInputAsU16(QLineEdit* lineEdit)
{
    auto numStr = lineEdit->text();
    return std::atoi(numStr.toStdString().c_str());
}

int16_t Widget::getTextInputAsInt16(QLineEdit* lineEdit)
{
    auto numStr = lineEdit->text();
    return std::atoi(numStr.toStdString().c_str());
}

bool Widget::createIOValueFromComboBox(QComboBox* vomboBox)
{
    return vomboBox->currentIndex() > 0;
}


bool Widget::createDiagValueFromComboBox(QComboBox* vomboBox)
{
    return vomboBox->currentIndex() > 0;
}

void Widget::initUi()
{
    itsUsageMode = ui->comboBox_4;
    itsWeakupState = ui->comboBox_5;

    itsDaytimeLampOnCmd = ui->comboBox_16;
    itsDayTimeLampLcuUartcanCommunicationIssue = ui->comboBox_12;
    itsDRL_LED_Issue = ui->comboBox_15;
    itsFLDaytimeLampOnSt = ui->label_32;
    itsFLDaytimeLampFlt = ui->label_30;

    itsDlpModuls.push_back(ui->Dlp);
    itsIO_LM3409_EN = ui->comboBox_41;
    its_DO_S32K_SL3409_PWM = ui->lineEdit;
    its_motor_Angle = ui->lineEdit_4;
    itsPubProjectionAngleAdjstReq = ui->comboBox_43;

    itsHiBeam.push_back(ui->HiBeam);
    itsHiBeamOnCmd = ui->comboBox_35;
//    itsAdpvHiBeamFctOnCmd = ui->comboBox_34;
    itsHiBeamBuckChannelIssue = ui->comboBox_33;
    itsHiBeamLedDevIssue = ui->comboBox_36;
    itsHiBeamBuckOutputIssue = ui->comboBox_37;
    itsLeHiBeamOnSt = ui->label_58;
    itsLeHiBeamFlt = ui->label_57;


    itsLeftFrontTurnLamps.push_back(ui->LeftFrontTurnLamp_0);
    itsLeftFrontTurnLamps.push_back(ui->LeftFrontTurnLamp_1);
    itsLeftFrontTurnLamps.push_back(ui->LeftFrontTurnLamp_2);
    itsLeftFrontTurnLamps.push_back(ui->LeftFrontTurnLamp_3);
    itsLeftFrontTurnLamps.push_back(ui->LeftFrontTurnLamp_4);
    itsLeftFrontTurnLamps.push_back(ui->LeftFrontTurnLamp_5);
    itsLeftFrontTurnLamps.push_back(ui->LeftFrontTurnLamp_6);
    itsLeftFrontTurnLamps.push_back(ui->LeftFrontTurnLamp_7);
    itsLeftFrontTurnLamps.push_back(ui->LeftFrontTurnLamp_8);
    itsLeftFrontTurnLamps.push_back(ui->LeftFrontTurnLamp_9);
    itsLeftFrontTurnLamps.push_back(ui->LeftFrontTurnLamp_10);
    itsLeftFrontTurnLamps.push_back(ui->LeftFrontTurnLamp_11);
    itsLeftFrontTurnLamps.push_back(ui->LeftFrontTurnLamp_12);
    itsLeftFrontTurnLamps.push_back(ui->LeftFrontTurnLamp_13);
    itsLeftFrontTurnLamps.push_back(ui->LeftFrontTurnLamp_14);
    itsLeftFrontTurnLamps.push_back(ui->LeftFrontTurnLamp_15);
    itsLeftFrontTurnLamps.push_back(ui->LeftFrontTurnLamp_16);
    itsLeftFrontTurnLamps.push_back(ui->LeftFrontTurnLamp_17);
    itsLeftFrontTurnLamps.push_back(ui->LeftFrontTurnLamp_18);
    itsLeftFrontTurnLamps.push_back(ui->LeftFrontTurnLamp_19);
    itsLeftFrontTurnLamps.push_back(ui->LeftFrontTurnLamp_20);
    itsLeftFrontTurnLamps.push_back(ui->LeftFrontTurnLamp_21);
    itsLeftFrontTurnLamps.push_back(ui->LeftFrontTurnLamp_22);
    itsLeftFrontTurnLamps.push_back(ui->LeftFrontTurnLamp_23);
    itsLeftFrontTurnLamps.push_back(ui->LeftFrontTurnLamp_24);
    itsLeftFrontTurnLamps.push_back(ui->LeftFrontTurnLamp_25);
    itsLeftFrontTurnLamps.push_back(ui->LeftFrontTurnLamp_26);
    itsLeftFrontTurnLamps.push_back(ui->LeftFrontTurnLamp_27);
    itsLeftFrontTurnLamps.push_back(ui->LeftFrontTurnLamp_28);
    itsLeftFrontTurnLamps.push_back(ui->LeftFrontTurnLamp_29);
    itsLeftFrontTurnLamps.push_back(ui->LeftFrontTurnLamp_30);
    itsLeftFrontTurnLamps.push_back(ui->LeftFrontTurnLamp_31);
    itsTL_EN = ui->comboBox;
    itsTL_Flow = ui->comboBox_2;
    itsTurnLampOccupySts = ui->comboBox_9;
    itsLeFrntTurnLampLcuUartcanCommunicationIssue = ui->comboBox_3;
    itsTL_SW_OutputIssue = ui->comboBox_6;
    itsLeFrntTurnLampLedIssue = ui->comboBox_7;
    itsLeFrntTurnLampOnSt = ui->label_11;
    itsLeFrntTurnLampFlt = ui->label_10;

    itsLoBeam.push_back(ui->LoBeam);
    itsLoBeamOnCmd = ui->comboBox_26;
    itsLoBeamBuckChannelIssue = ui->comboBox_30;
    itsLoBeamLedDevIssue = ui->comboBox_31;
    itsLoBeamBuckOutputIssue = ui->comboBox_32;
    itsLoBeamPowerSupplyIssue = ui->comboBox_29;
    itsLeLoBeamOnSt = ui->label_48;
    itsLeLoBeamFlt = ui->label_49;

    itsPO2_LED.push_back(ui->PO2_LED);

    itsDatTimeAndPosLamps.push_back(ui->PosnLamp_0);
    itsDatTimeAndPosLamps.push_back(ui->PosnLamp_1);
    itsDatTimeAndPosLamps.push_back(ui->PosnLamp_2);
    itsDatTimeAndPosLamps.push_back(ui->PosnLamp_3);
    itsDatTimeAndPosLamps.push_back(ui->PosnLamp_4);
    itsDatTimeAndPosLamps.push_back(ui->PosnLamp_5);
    itsDatTimeAndPosLamps.push_back(ui->PosnLamp_6);
    itsDatTimeAndPosLamps.push_back(ui->PosnLamp_7);
    itsDatTimeAndPosLamps.push_back(ui->PosnLamp_8);
    itsDatTimeAndPosLamps.push_back(ui->PosnLamp_9);
    itsDatTimeAndPosLamps.push_back(ui->PosnLamp_10);
    itsDatTimeAndPosLamps.push_back(ui->PosnLamp_11);
    itsDatTimeAndPosLamps.push_back(ui->PosnLamp_12);
    itsDatTimeAndPosLamps.push_back(ui->PosnLamp_13);
    itsDatTimeAndPosLamps.push_back(ui->PosnLamp_14);
    itsDatTimeAndPosLamps.push_back(ui->PosnLamp_15);
    itsDatTimeAndPosLamps.push_back(ui->PosnLamp_16);
    itsDatTimeAndPosLamps.push_back(ui->PosnLamp_17);
    itsDatTimeAndPosLamps.push_back(ui->PosnLamp_18);
    itsDatTimeAndPosLamps.push_back(ui->PosnLamp_19);
    itsDatTimeAndPosLamps.push_back(ui->PosnLamp_20);
    itsDatTimeAndPosLamps.push_back(ui->PosnLamp_21);
    itsDatTimeAndPosLamps.push_back(ui->PosnLamp_22);
    itsDatTimeAndPosLamps.push_back(ui->PosnLamp_23);
    itsDatTimeAndPosLamps.push_back(ui->PosnLamp_24);
    itsDatTimeAndPosLamps.push_back(ui->PosnLamp_25);
    itsDatTimeAndPosLamps.push_back(ui->PosnLamp_26);
    itsDatTimeAndPosLamps.push_back(ui->PosnLamp_27);
    itsDatTimeAndPosLamps.push_back(ui->PosnLamp_28);
    itsDatTimeAndPosLamps.push_back(ui->PosnLamp_29);
    itsDatTimeAndPosLamps.push_back(ui->PosnLamp_30);
    itsDatTimeAndPosLamps.push_back(ui->PosnLamp_31);
    itsFLPosnLampOnCmd = ui->comboBox_11;
    itsFLPosnLampBriSet = ui->comboBox_8;
//    itsFLPosnLampLcu_UartcanCommunicationIssue = ui->comboBox_12;
    itsPW_SW_OutputIssue = ui->comboBox_13;
    itsLeFrntPosnLampOnSt = ui->label_19;
    itsLeFrntPosnLampBri = ui->label_20;
    itsLeFrntPosnLampFlt = ui->label_25;

    itsStarrySkyLamps.push_back(ui->StarrySkyLamp_0);
    itsStarrySkyLamps.push_back(ui->StarrySkyLamp_1);
    itsStarrySkyLamps.push_back(ui->StarrySkyLamp_2);
    itsStarrySkyLamps.push_back(ui->StarrySkyLamp_3);
    itsStarrySkyLamps.push_back(ui->StarrySkyLamp_4);
    itsStarrySkyLamps.push_back(ui->StarrySkyLamp_5);
    itsStarrySkyLamps.push_back(ui->StarrySkyLamp_6);
    itsStarrySkyLamps.push_back(ui->StarrySkyLamp_7);
    itsStarrySkyLamps.push_back(ui->StarrySkyLamp_8);
    itsStarrySkyLamps.push_back(ui->StarrySkyLamp_9);

    itsCornerLamp.push_back(ui->CornerLamp);
    itsAFSEna = ui->comboBox_40;
    its_VehicleSpeed = ui->lineEdit_2;
    its_SteeringWheelAngle = ui->lineEdit_3;
    itsCL_SW_SwitchIssue = ui->comboBox_42;
    itsLCU_FL_FLCornerLampOnSt = ui->label_65;
    itsLCU_FL_FLCornerLampFlt = ui->label_64;

    itsLoBeamEffectCtrlCmd = ui->comboBox_38;
    itsLoBeamEffectSel = ui->comboBox_44;
    itsFrntRibbonLampEffectCtrlCmd = ui->comboBox_48;
    itsFrntRibbonLampEffectModSet = ui->comboBox_49;
    itsFrntRibbonLampEffectSel = ui->comboBox_50;
    itsFrntRibbonLampEffectShowSt = ui->label_28;
    itsFrntRibbonLampEffectSt = ui->label_34;
    itsLoBeamEffectShowSt = ui->label_36;
    itsLoBeamEffectSt = ui->label_38;

    //test
//    itsUsageMode->setCurrentIndex(1);
//    ui->comboBox_38->setCurrentIndex(3);
//    ui->comboBox_39->setCurrentIndex(3);
//    ui->comboBox_44->setCurrentIndex(2);
//    ui->comboBox_48->setCurrentIndex(3);
//    ui->comboBox_49->setCurrentIndex(3);
//    ui->comboBox_50->setCurrentIndex(2);
    itsFLPosnLampBriSet->setCurrentIndex(1);

}

void Widget::on_comboBox_currentIndexChanged(int index)
{
    // itsTurnLampOccupySts->setCurrentIndex(index);
}


void Widget::on_comboBox_45_currentIndexChanged(int index)
{
    switch(index)
    {
        case 0:
        {
            ui->comboBox_38->setCurrentIndex(0);
            ui->comboBox_44->setCurrentIndex(0);
            ui->comboBox_48->setCurrentIndex(0);
            ui->comboBox_49->setCurrentIndex(0);
            ui->comboBox_50->setCurrentIndex(0);
            break;
        }
        case 1:
        {

            ui->comboBox_38->setCurrentIndex(3);
            ui->comboBox_44->setCurrentIndex(2);
            ui->comboBox_48->setCurrentIndex(3);
            ui->comboBox_49->setCurrentIndex(2);
            ui->comboBox_50->setCurrentIndex(2);
            break;
        }

        case 2:
        {
            ui->comboBox_38->setCurrentIndex(3);
            ui->comboBox_44->setCurrentIndex(2);
            ui->comboBox_48->setCurrentIndex(3);
            ui->comboBox_49->setCurrentIndex(3);
            ui->comboBox_50->setCurrentIndex(2);
            break;
        }


        case 3:
        {
            ui->comboBox_38->setCurrentIndex(3);
            ui->comboBox_44->setCurrentIndex(8);
            ui->comboBox_48->setCurrentIndex(3);
            ui->comboBox_49->setCurrentIndex(2);
            ui->comboBox_50->setCurrentIndex(8);
            break;
        }


        case 4:
        {
            ui->comboBox_38->setCurrentIndex(3);
            ui->comboBox_44->setCurrentIndex(8);
            ui->comboBox_48->setCurrentIndex(3);
            ui->comboBox_49->setCurrentIndex(3);
            ui->comboBox_50->setCurrentIndex(8);
            break;
        }

        default:
        {
            break;
        }
    }
}

